#include "Result.h"

CCScene* ResultLayer::scene(GameLayer* parentLayer)
{
    CCScene *scene = CCScene::create();
    
    ResultLayer *layer = ResultLayer::create();

	parentLayer->setTouchEnabled(false);
    layer->score = parentLayer->score;
    layer->lblScore->setString(CCString::createWithFormat("%d", layer->score)->getCString());
	layer->registerScore();
	
	scene->addChild(layer);
    
    return scene;
}


bool ResultLayer::init()
{
    if(!CCLayerColor::initWithColor(ccc4(255, 255, 255, 0)))
    {
        return false;
    }

	bEndConnection = false;
	connectionTime = 0;
    winSize = CCDirector::sharedDirector()->getWinSize();
    
    dimLayer = CCLayerColor::create(ccc4(0, 0, 0, 100), winSize.width, winSize.height);
    dimLayer->setAnchorPoint(CCPointZero);
    dimLayer->setPosition(CCPointZero);
    this->addChild(dimLayer);
    
    popLayer = CCLayerColor::create(ccc4(0, 255, 0, 255), 400, 400);
    popLayer->setAnchorPoint(CCPointZero);
    popLayer->setPosition(ccp((winSize.width - popLayer->getContentSize().width)/2, (winSize.height - popLayer->getContentSize().height)/2));
    this->addChild(popLayer);
    
	lblScore = CCLabelTTF::create(CCString::createWithFormat("%d", score)->getCString(), "∏º¿∫∞ÌµÒ", 40);
//	lblScore->setColor(ccBLACK);
	lblScore->setPosition((ccp(winSize.width * 0.5f, winSize.height * 0.6f)));
	this->addChild(lblScore, 10);

	this->schedule(schedule_selector(ResultLayer::update), 1.0f);

	
   
    return true;
}

void ResultLayer::update(float dt)
{
	if(connectionTime >= 60)
	{
		SceneController::runScene(MENU_SCENE);
	}
	connectionTime++;
}


void ResultLayer::doQuit()
{
    CCDirector::sharedDirector()->resume();
    SceneController::runScene(MENU_SCENE);
}

void ResultLayer::doRestart()
{
    CCDirector::sharedDirector()->resume();
    SceneController::runScene(GAME_SCENE);
}

void ResultLayer::registerScore()
{
	std::string userCode = CCUserDefault::sharedUserDefault()->getStringForKey("userCode");

	CCString* url = CCString::createWithFormat("http://115.71.238.29:8080/SegmentWeb/registerScore/?userCode=%s&score=%d", userCode.c_str(), score);

	CCHttpRequest* request = new CCHttpRequest();
	request->setUrl(url->getCString());
	request->setRequestType(CCHttpRequest::kHttpGet);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json; charset=utf-8");
	request->setHeaders(headers);
	request->setResponseCallback(this, callfuncND_selector(ResultLayer::registerScoreCallBack));

	request->setTag("registerScore");
	CCHttpClient::getInstance()->send(request);
	request->release();

    
    CCMenu* pMenu = CCMenu::create();
	CCLabelTTF* pLabel = CCLabelTTF::create("QUIT", "∏º¿∫ ∞ÌµÒ", 40);
	CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(pLabel, this, menu_selector(ResultLayer::doQuit));
	pMenuItem->setPosition(ccp(winSize.width * 0.4f, winSize.height * 0.4f));
	pMenu->addChild(pMenuItem);
    
	pLabel = CCLabelTTF::create("RESTART", "∏º¿∫ ∞ÌµÒ", 40);
	pMenuItem = CCMenuItemLabel::create(pLabel, this, menu_selector(ResultLayer::doRestart));
	pMenuItem->setPosition( ccp(winSize.width * 0.6f, winSize.height * 0.4f));
	pMenu->addChild(pMenuItem);
    
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu);
}

void ResultLayer::registerScoreCallBack(CCNode *sender, void *data)
{
	CCHttpResponse *response = (CCHttpResponse*)data;

	if(!response)
	{
		return;
	}

	if(!response->isSucceed())
	{
		CCMessageBox(response->getErrorBuffer(),"Response Error");
		return;
	}
	
	/*
	std::vector<char> *buffer = response->getResponseData();
	char* rcvData = (char*)malloc(buffer->size()+1);
	std::string s2(buffer->begin(), buffer->end());

	strcpy(rcvData, s2.c_str());

	
	std::string clearData((const char*)rcvData);
	size_t pos = clearData.rfind("}");
	clearData = clearData.substr(0, pos+1);
	
	if(clearData == "")
	{
		CCMessageBox("데이터가 없습니다.", "Error");
		return;
	}
	else
	{
	
		bEndConnection = true;
	}
	*/
}